//
//  SearchViewController.m
//  xxShare
//
//  Created by 柳麟喆 on 2018/4/1.
//  Copyright © 2018年 lzLiu. All rights reserved.
//

#import "SearchViewController.h"
#import "LZSegmentView.h"
#import "ResultViewController.h"
#import "FeedModel.h"

@interface SearchViewController ()<UISearchBarDelegate, LZSegmentViewDelegate>

@property (nonatomic, strong)UISearchBar *searchBar;
@property (nonatomic, strong)LZSegmentView *segment;
@property (nonatomic, copy)ResultViewController *nameVC;//搜索的用户名结果
@property (nonatomic, copy)ResultViewController *tagVC;//搜索的标签结果
@property (nonatomic, copy)ResultViewController *postVC;//搜索的发表结果

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUp];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
//    if (!_searchBar.isFirstResponder && _segment) {
//        [self.searchBar becomeFirstResponder];
//    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.searchBar resignFirstResponder];
}

#pragma mark - *******UISearchBarDelegate******* 
//- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
//    return YES;
//}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    [self.searchBar resignFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [_searchBar resignFirstResponder];
    [SVProgressHUD showWithStatus:@"加载中..."];
    ///假数据
    _nameVC.testData = [self addTestData:0];
    _nameVC.type = 0;
    [_nameVC.tableview reloadData];
    
    [self.view addSubview:_segment];
    [SVProgressHUD dismissWithDelay:1.0];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [_searchBar resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - *******LZDelegate*******
- (void)eventWhenScrollSubViewWithIndex:(NSInteger)index{
    if (index != 0) {
        _postVC.testData = [self addTestData:2];
        _postVC.type = 2;
        [_postVC.tableview reloadData];
        
        _tagVC.testData = [self addTestData:1];
        _tagVC.type = 1;
        [_tagVC.tableview reloadData];
    }
}

#pragma mark - *******私有方法*******
- (void)setUp{
    self.view.backgroundColor = BACKGROUND_COLOR;
    //隐藏导航栏上的返回按钮
    [self.navigationItem setHidesBackButton:YES];
    //用来放searchBar的View
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(5, 7, SCREEN_WIDTH, 30)];
    //创建searchBar
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(titleView.frame) - 15, 30)];
    //默认提示文字
    searchBar.placeholder = @"搜索用户名、内容或者标签";
    //代理
    searchBar.delegate = self;
    //显示右侧取消按钮
    searchBar.showsCancelButton = YES;
    //光标颜色
    searchBar.tintColor = [UIColor blueColor];
    //拿到searchBar的输入框
    UITextField *searchTextField = [searchBar valueForKey:@"_searchField"];
    //字体大小
    searchTextField.font = [UIFont systemFontOfSize:15];
    //输入框背景颜色
    searchTextField.backgroundColor = [UIColor colorWithRed:234/255.0 green:235/255.0 blue:237/255.0 alpha:1];
    //拿到取消按钮
    UIButton *cancleBtn = [searchBar valueForKey:@"cancelButton"];
    //设置按钮上的文字
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    //设置按钮上文字的颜色
    [cancleBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [titleView addSubview:searchBar];
    _searchBar = searchBar;
    self.navigationItem.titleView = titleView;
    
    ///segment of 用户名、标签、内容
    NSMutableArray *vcArray = [[NSMutableArray alloc]init];
    NSArray *findArray=  @[@"用户名",@"标签",@"内容"];
    
    _nameVC = [[ResultViewController alloc]init];
    _tagVC = [[ResultViewController alloc]init];
    _postVC = [[ResultViewController alloc]init];
    
    _nameVC.title = findArray[0];
    _tagVC.title = findArray[1];
    _postVC.title = findArray[2];
    
    [vcArray addObject:_nameVC];
    [vcArray addObject:_tagVC];
    [vcArray addObject:_postVC];
    
    [self addChildViewController:_nameVC];
    [self addChildViewController:_tagVC];
    [self addChildViewController:_postVC];
    
    _segment = [[LZSegmentView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) andControllers:vcArray];
    _segment.eventDelegate = self;
}

#pragma mark - *******假数据*******
- (NSMutableArray *)addTestData: (NSInteger)type{
    NSMutableArray *Data = [[NSMutableArray alloc]init];
    NSArray *introduce = @[@"爱分享美食的王子",@"肚子饿了找我就对了",@"关注大V免费送沙拉酱",@"乐于助人"];
    int num = type == 0 ? 5 : 3;
    for (int i = 1; i < num; i++) {
        NSDictionary *dic = @{@"accountID":[NSString stringWithFormat:@"ID:%d",1973*i*58],
                              @"imageName":[NSString stringWithFormat:@"avator%d.png",i],
                              @"name":[NSString stringWithFormat:@"小美食%d",i*9],
                              @"devote":@"30",
                              @"photos":@[[NSString stringWithFormat:@"美食%d.png",i+4],[NSString stringWithFormat:@"美食%d.png",i]],
                              @"postContent":introduce[i-1],
                              @"location":@"杭州市",
                              @"time":@"7小时前",
                              @"moneyNum":@"231",
                              @"thumbNum":@"321",
                              @"commentNum":@"123",
                              @"tags":@"美食",
                              @"width":[NSString stringWithFormat:@"%d",i*100],
                              @"height":[NSString stringWithFormat:@"%d",i*80],
                              };
        FeedModel *model = [[FeedModel alloc]initWithFeedModel:dic];
        [Data addObject:model];
    }
    return Data;
}

@end
